/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cardarus.operatingsystem.struts.actions;

import com.cardarus.operatingsystem.domain.SalesTransaction;
import com.cardarus.operatingsystem.domain.SalesTransactionRow;
import com.cardarus.operatingsystem.model.BusinessModel;
import com.cardarus.operatingsystem.struts.forms.CheckoutForm;
import java.util.Arrays;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

/**
 * Deletes selected rows from the sales transaction.
 * This action is called from the Struts framework.
 * 
 * @author Javier Snaider
 */
public class DeleteCheckoutRowAction extends org.apache.struts.action.Action {

    private static final Logger logger = Logger.getLogger(DeleteCheckoutRowAction.class.getCanonicalName());
    private static final String[] EMPTYARRAY = {};

    /**
     * This action is called from the Struts framework.
     *
     * Deletes selected rows from the sales transaction.
     *
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        CheckoutForm cform = (CheckoutForm) form;
        ActionErrors errors = new ActionErrors();

        BusinessModel model = BusinessModel.getInstance();
        HttpSession session = request.getSession();

        SalesTransaction sale = (SalesTransaction) session.getAttribute("sale");
        List<SalesTransactionRow> rows = sale.getRows();

        for (String r : cform.getSelectedRows()) {
            int rowNumber = Integer.parseInt(r);
            for (SalesTransactionRow row : rows) {
                if (row.getRowNumber() == rowNumber) {
                    rows.remove(row);
                    break;
                }
            }
        }
        //Renumber all rows
        int i = 0;
        for (SalesTransactionRow row : rows) {
            row.setRowNumber(i++);
        }

        double sub = 0.0;
                    for (SalesTransactionRow row : rows) {
                       sub = sub + row.getPrice();
                    }

        sale.setSubTotal(sub);
        model.calculateTotal(sale);

        logger.log(Level.INFO, Arrays.toString(cform.getSelectedRows()));

        cform.setSelectedRows(EMPTYARRAY);
        return mapping.findForward("success");
    }
}
